package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/7.<br><br>
 *
 * 买卖股票的最佳时机 II
 */
public class Topic121 {
    public static void main(String[] args) {
        Topic121 instance = new Topic121();

        int[] array = new int[] {7,1,5,3,6,4};
        System.out.println(instance.maxProfit(array));

        array = new int[] {1,2,3,4,5};
        System.out.println(instance.maxProfit(array));

        array = new int[] {7,6,4,3,1};
        System.out.println(instance.maxProfit(array));
    }

    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        int maxProfit = 0;

        for (int price : prices) {
            if (price < cost) {
                cost = price;
            }

            maxProfit = Math.max(maxProfit, price - cost);
        }

        return maxProfit;
    }
}
